package com.redstars.generate.controller;

import com.redstars.generate.page.PageResult;
import com.redstars.generate.result.Result;
import com.redstars.generate.service.SysTableFieldService;
import com.redstars.generate.vo.SysTableFieldVo;
import com.redstars.generate.vo.SysTableVo;
import lombok.AllArgsConstructor;
import com.redstars.generate.service.SysTableService;
import com.redstars.generate.query.SysTableQuery;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
* 代码生成表
*
* @author zhouhx 
* @since 1.0.0 2022-10-05
*/
@RestController
@RequestMapping("gen/table")
@AllArgsConstructor
public class SysTableController {
    private final SysTableService sysTableService;
    private final SysTableFieldService sysTableFieldService;
    /* *
     *
     * @author zhuohx
     * @description 分页
     * @parms  [query]
     * @return Result<PageResult<SysTableVo>>
     * @throws
     * @date 2022/10/5 15:52
     */
    @GetMapping("page")
    public Result<PageResult<SysTableVo>> page(@Valid SysTableQuery query){
        PageResult<SysTableVo> page = sysTableService.page(query);

        return Result.ok(page);
    }

    /* *
     *
     * @author zhuohx
     * @description 详情
     * @parms  [query]
     * @return com.redstars.generate.result.Result<com.redstars.generate.vo.SysTableVo>
     * @throws
     * @date 2022/10/5 15:52
     */
    @GetMapping("{id}")
    public Result<SysTableVo> detail(@PathVariable("id") Long id){
        SysTableVo detail = sysTableService.detail(id);
        return Result.ok(detail);
    }

    /* *
     *
     * @author zhuohx
     * @description 保存
     * @parms  [vo]
     * @return com.redstars.generate.result.Result<java.lang.String>
     * @throws
     * @date 2022/10/5 15:52
     */
    @PostMapping
    public Result<String> save(@RequestBody SysTableVo vo){
        sysTableService.save(vo);

        return Result.ok();
    }

    /* *
     *
     * @author zhuohx
     * @description 修改
     * @parms  [vo]
     * @return com.redstars.generate.result.Result<java.lang.String>
     * @throws
     * @date 2022/10/5 15:52
     */
    @PutMapping
    public Result<String> update(@RequestBody @Valid SysTableVo vo){
        sysTableService.update(vo);

        return Result.ok();
    }

    /* *
     *
     * @author zhuohx
     * @description 删除
     * @parms  [idList]
     * @return com.redstars.generate.result.Result<java.lang.String>
     * @throws
     * @date 2022/10/5 15:52
     */
    @DeleteMapping
    public Result<String> delete(@RequestBody List<Long> idList){
        sysTableService.delete(idList);

        return Result.ok();
    }

    /* *
     *
     * @author zhuohx
     * @description 同步表结构
     * @parms  [id 表ID]
     * @return com.redstars.generate.result.Result<java.lang.String>
     * @throws
     * @date 2022/10/6 8:54
     */
    @PostMapping("sync/{id}")
    public Result<String> sync(@PathVariable("id") Long id) {
        sysTableService.sync(id);

        return Result.ok();
    }

    /* *
     *
     * @author zhuohx
     * @description 导入表结构
     * @parms  [datasourceId 数据源ID, tableNameList表名列表]
     * @return com.redstars.generate.result.Result<java.lang.String> 
     * @throws 
     * @date 2022/10/6 8:54
     */
    @PostMapping("import/{datasourceId}")
    public Result<String> tableImport(@PathVariable("datasourceId") Long datasourceId, @RequestBody List<String> tableNameList) {
        sysTableService.tableImport(datasourceId,tableNameList);

        return Result.ok();
    }

    /* *
     *
     * @author zhuohx
     * @description 修改表字段数据
     * @parms  [tableId 表ID, tableFieldList字段列表]
     * @return com.redstars.generate.result.Result<java.lang.String>
     * @throws
     * @date 2022/10/6 8:55
     */
    @PutMapping("field/{tableId}")
    public Result<String> updateTableField(@PathVariable("tableId") Long tableId, @RequestBody List<SysTableFieldVo> tableFieldList) {
        sysTableFieldService.updateTableField(tableId, tableFieldList);

        return Result.ok();
    }

    /* *
     *
     * @author zhuohx
     * @description 列表查询
     * @parms  [query]
     * @return com.redstars.generate.result.Result<java.util.List<com.redstars.generate.vo.SysTableVo>>
     * @throws
     * @date 2022/11/8 15:07
     */
    @GetMapping("list")
    public Result<List<SysTableVo>> list(SysTableQuery query){
        List<SysTableVo> list = sysTableService.list(query);
        return Result.ok(list);
    }
}